Skip to content

feat: compose contracts together with the source supergraph - #347

Merged
n1ru4l merged 5 commits into
mainfrom
kamil-contracts
Dec 1, 2026
Merged

n1ru4l merged 5 commits into
mainfrom
kamil-contracts

Conversation

@n1ru4l

@n1ru4l n1ru4l commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Every contract used to be a full composition. A contract only adds @inaccessible to a graph that already composed, so most of that work is redundant.

Changes

composeServicesWithContracts(services, contracts) composes once, then for each contract:

  1. applies the tag filter to the supergraph state, recording each change;
  2. runs only the rules that depend on @inaccessible (REFERENCED_INACCESSIBLE, REQUIRED_INACCESSIBLE, ONLY_INACCESSIBLE_CHILDREN, …);
  3. builds the document, then restores the state.

Output and errors equal composeServices + composeSchemaContract per contract. If the subgraphs don't compose, it falls back to composing each contract from the subgraphs.

The filter mirrors the subgraph filter, including what the merged state can't see: per-subgraph type names (renamed roots), type-level @external (Fed v1), @tag the filter leaves in place, federation types declared in subgraphs, and QUERY_ROOT_TYPE_INACCESSIBLE.

Shared filter rules: both filters (the subgraph transform behind composeSchemaContract and the state filter) make every include/exclude decision through the same functions in tag-extraction.ts: isExcludedByFilter, isExcludedByExcludeFilter, and isArgumentExcludedByFilter (untagged arguments inherit field tags for include, from #345). A rule change lands in both paths at once.

Shared definitions: a contract document reuses each supergraph definition that prints the same, which is almost all of them (4,279 / 4,282 on the huge fixture).

composeSchemaContract(..., { skipSatisfiability }): satisfiability ignores inaccessible fields in required paths but still uses them to resolve, so a contract of a graph that composed is satisfiable. Opt-in: only safe after the base composition succeeded.

Results

Huge fixture (191 subgraphs), compared with main (composeServices + composeSchemaContract per contract), minimum heap / median time:

contracts main this PR
0 138 MB / 1.20 s 138 MB / 1.15 s
1 165 MB / 2.14 s 134 MB / 1.42 s
3 208 MB / 3.69 s 134 MB / 1.69 s
8 334 MB / 8.45 s 145 MB / 2.81 s

At 8 contracts: −57% heap, ~3× faster. Each contract costs ~25 MB / 0.9 s on main and ~0.2 s here. Timings were taken on a loaded machine (relative numbers hold, absolute ones are inflated). pnpm bench now includes contract groups.

Testing

compose-services-with-contracts.spec.ts checks results against the old path: one case per behaviour above, contract-only errors, no leaking between contracts, definition sharing, and argument tag inheritance.

A drift guard puts random tags on the DGS and stars-stuff fixtures at three densities × three seeds and compares both paths under include, exclude and mixed filters (~6 s). It fails when either filter's argument rule or type rule is changed on its own.

Follow-ups

  • Hive must switch to composeServicesWithContracts.
  • Composition bug, also on main: @inaccessible on an interface field argument is dropped from the supergraph. A contract that excludes such an argument leaks it into the API schema. Separate PR.
  • Remove the quirks the state filter mirrors (federation types enabling the inaccessible spec, @tag left on directive definitions and scalar/union extensions, the v1 type-level @external mismatch), then make composeSchemaContract use the state path and delete the subgraph transform.
  • Pre-existing in both paths: removing unreachable types can throw Unknown type instead of returning an error.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@theguild/federation-composition 0.28.0-alpha-20261119164620-21d86525b09fca0ba1fa74aed0e82fc6eb9ca46b npm ↗︎ unpkg ↗︎

@n1ru4l n1ru4l changed the title feat: compose contracts together with the source sueprgraph feat: compose contracts together with the source supergraph Nov 19, 2026
A contract only adds @inaccessible to an already composed graph.
Satisfiability ignores inaccessible fields in the paths it requires
while still using them to resolve those paths, so a contract of a
satisfiable graph is satisfiable too. When the base graph composed
successfully, callers can now skip the check, which is the most
expensive part of composing each contract.

Also adds tests for the errors only a contract can introduce
(REFERENCED_INACCESSIBLE, REQUIRED_INACCESSIBLE,
ONLY_INACCESSIBLE_CHILDREN), run with and without the skip.
Add composeServicesWithContracts, which composes the supergraph once
and derives each contract from its state instead of composing all the
subgraphs again. The tag filter is applied to the supergraph state with
an undo log, only the rules about @inaccessible run, and the state is
restored for the next contract. Errors and output are the same as
composeServices followed by composeSchemaContract for each contract.

The filter mirrors the decisions the subgraph filter makes, including
the ones the merged state cannot tell on its own: tags matched by the
type names used in each subgraph (renamed root types), type-level
@external in Federation v1, @tag the filter leaves in place, federation
types declared in subgraphs, and QUERY_ROOT_TYPE_INACCESSIBLE reported
by subgraph validation.

When the subgraphs fail to compose, each contract is composed from the
subgraphs as before.
A contract's supergraph document is almost entirely the same as the
supergraph it is derived from, yet every contract kept its own copy of
each definition. A contract document now reuses the definitions of the
supergraph document that print the same, so each additional contract
adds little memory: base + 8 contracts on the huge-schema fixture needs
145 MB of heap instead of 279 MB.

Add benchmarks of a medium and a huge schema with 3 contracts,
comparing composeServices followed by composeSchemaContract per
contract with composeServicesWithContracts.
Mirrors the include-filter argument behavior from #345 in the state-based contract filter.
The subgraph transform and the supergraph state filter now make every include/exclude decision through the same functions, so a rule change (like argument tag inheritance) lands in both. The random-tags test now covers several tag densities and a second fixture to catch any remaining drift.
@n1ru4l
n1ru4l marked this pull request as ready for review December 1, 2026 11:56
@n1ru4l
n1ru4l merged commit 4b1748e into main Dec 1, 2026
5 checks passed
@n1ru4l
n1ru4l deleted the kamil-contracts branch December 1, 2026 12:10
@github-actions github-actions Bot mentioned this pull request Dec 1, 2026
n1ru4l pushed a commit that referenced this pull request Dec 1, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @theguild/federation-composition@0.28.0

### Minor Changes

-
[#347](#347)
[`4b1748e`](4b1748e)
Thanks [@n1ru4l](https://github.com/n1ru4l)! - Add a
`skipSatisfiability` option to `composeSchemaContract`. A contract only
adds `@inaccessible` to an already composed graph, so when the same
services composed successfully without the contract, the satisfiability
check cannot find anything new. Skipping it cuts roughly 45% of the time
spent composing each contract.

    ```ts
    const base = composeServices(services);

    if (!base.errors) {
      const contract = composeSchemaContract(services, filter, true, {
        skipSatisfiability: true,
      });
    }
    ```

The `@inaccessible` rules that contracts can trigger (for example
`REFERENCED_INACCESSIBLE`, `REQUIRED_INACCESSIBLE` and
`ONLY_INACCESSIBLE_CHILDREN`) keep running.

-
[#347](#347)
[`4b1748e`](4b1748e)
Thanks [@n1ru4l](https://github.com/n1ru4l)! - Add
`composeServicesWithContracts` to compose a supergraph together with its
schema contracts.

Instead of composing all the subgraphs again for every contract, each
contract is derived from the composed supergraph: the tag filter is
applied to it, and only the rules about `@inaccessible` run, since those
are the only ones a contract can fail. The results are the same as
`composeServices` followed by `composeSchemaContract` for each contract,
at a fraction of the time and memory.
The document of a contract reuses the definitions it has in common with
the supergraph's document, so each contract adds little memory, however
many there are.

    ```ts
const { supergraph, contracts } = composeServicesWithContracts(services,
[
      { filter: { include: new Set(["public"]), exclude: new Set() } },
      {
        filter: { include: new Set(), exclude: new Set(["internal"]) },
        removeUnreachableTypes: false,
      },
    ]);
    ```

When the subgraphs fail to compose, each contract is composed from the
subgraphs, as before.

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
kamilkisiela added a commit to graphql-hive/federation-composition-rs that referenced this pull request Dec 3, 2026
Ports upstream
[v0.27.3](https://github.com/graphql-hive/federation-composition/releases/tag/v0.27.3)
and
[v0.28.0](https://github.com/graphql-hive/federation-composition/releases/tag/v0.28.0),
one commit each, and moves `UPSTREAM` to `v0.28.0`.

## v0.27.3

- `DeprecatedInterfaceFieldImplementationRule`
(graphql-hive/federation-composition#349): a deprecated field whose
interface field is not deprecated fails composition with
`INVALID_GRAPHQL`, since graphql-js 17 rejects such a schema.
- The new `INVALID-GRAPHQL` test and the updated `huge-schema` fixture
are ported.

## v0.28.0

From graphql-hive/federation-composition#347:

- `composeSchemaContract` gains a `skipSatisfiability` option
(`ComposeSchemaContractOptions`; `compose_schema_contract` takes an
`options` argument).
- `composeServicesWithContracts` (new
`contracts/supergraph_state_contract.rs`) composes the supergraph once
and derives each contract from its state: the tag filter is applied to
the state, only the six `@inaccessible` rules run
(`validate_inaccessible_usage`), the supergraph is built again, and the
state is restored for the next contract. When the subgraphs fail to
compose, each contract is composed in full.
- The three filter predicates (`isExcludedByFilter`,
`isExcludedByExcludeFilter`, `isArgumentExcludedByFilter`) are shared by
both filters in `tag_extraction.rs`.
- The wasm bindings and the npm shim export
`composeServicesWithContracts` and `SchemaContract`. The shim shares a
contract's unchanged definitions with the supergraph's document, as
upstream does.

Where the Rust differs in mechanism, not in result:

- **External fields' `@tag`s.** Upstream reads them from the subgraph
states' types, which the Rust drops after merging each subgraph. A
stripped subgraph state now keeps its tagged fields' tags
(`SubgraphState::field_tags`); untagged fields cost nothing.
- **Restoring the state.** Upstream mutates the state through undo
closures. The Rust decides every change from the state read-only,
applies them, and logs each previous value by its position, so `restore`
puts it back.
- **"a contract reuses the definitions it does not change"** checks JS
object identity. It passes on wasm through the shim; the Rust test
compares the printed definitions, since a Rust success is its SDL.

## Verification

- `pnpm conformance` at v0.28.0: 7,401 passed, 25 skipped, 8 todo on
wasm (same as upstream's own TypeScript); 3,714/3,714 compositions
identical.
- `cargo test --workspace`: the suite has 3,823 passed, 17 ignored;
graphql-js differentials, doctests and unit tests pass. The random-tag
tests on `dgs` and `stars-stuff` compare every derived contract with the
full per-contract composition.
- `pnpm test:npm`: 16/16.
- clippy (native and wasm32) and `cargo doc`, with `-D warnings`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants